﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NetManager.Core.Services;
using NetManager.LocalBrowser.Services;
using System.IO;
using System.Windows.Controls.Primitives;

namespace NetManager.LocalBrowser.Controls
{
    /// <summary>
    /// Interaction logic for PathNavigatorControl.xaml
    /// </summary>
    public partial class PathNavigatorControl : UserControl
    {
        public PathNavigatorControl()
        {
            InitializeComponent();
            Navigator.KeyDown += new KeyEventHandler(Navigator_KeyDown);
        }

        void Navigator_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                CurrentPath = (sender as ComboBox).Text as string;
        }

        public string CurrentPath
        {
            set { SetValue(CurrentPathProperty, value); }
            get { return GetValue(CurrentPathProperty) as string; }
        }

        public static readonly DependencyProperty CurrentPathProperty = DependencyProperty.
            Register("CurrentPath", typeof(string), typeof(PathNavigatorControl),
            new FrameworkPropertyMetadata("C:\\", new PropertyChangedCallback(OnCurrentPathChanged)));

        private static void OnCurrentPathChanged(DependencyObject navigatorControl, DependencyPropertyChangedEventArgs args)
        {
            var control = navigatorControl as PathNavigatorControl;
            var contentService = new LocalContentService();
            
            control.Navigator.Items.Clear();
            control.Navigator.Text = args.NewValue as string;
            
            foreach (var dir in new DirectoryInfo(args.NewValue as string).GetDirectories())
            {
                control.Navigator.Items.Add(dir.FullName);
            }
        }


    }
}
